﻿using Microsoft.Practices.EnterpriseLibrary.Data;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Data.Entity.Core.Objects;
using System.Data.Entity.Infrastructure;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TN_CMS.Domain.Abstract;
using TN_CMS.Domain.Entities;

namespace TN_CMS.Domain.Concrete
{
    public class EFStaticContentGroupRepository : BaseRepository, IStaticContentGroupRepository
    {
        private EFDbContext context;

        public EFStaticContentGroupRepository(EFDbContext context = null)
        {
            this.context = (context != null) ? context : new EFDbContext();
        }

        public IQueryable<StaticContentGroup> StaticContentGroups
        {
            get { return context.StaticContentGroups; }
        }

        public IQueryable<StaticContentGroup> GetList(out int total, int pageIndex = 0, int pageSize = 10, bool getInactive = true)
        {
            var entries = StaticContentGroups
                .Where(a => a.IsActive == true || a.IsActive == !getInactive)
                .OrderBy(a => a.Id)
                .Skip((pageIndex - 1) * pageSize)
                .Take(pageSize);
            total = StaticContentGroups
                .Count(a => a.IsActive == true || a.IsActive == !getInactive);
            return entries;
        }

        public List<StaticContentGroup> GetList(int pageIndex, int pageSize, out int total, string orderClause, string whereClause)
        {

            var map = MapBuilder<StaticContentGroup>
                .MapAllProperties()
                .DoNotMap(m => m.StaticContents)
                .DoNotMap(m => m.CreatedByAdmin)
                .DoNotMap(m => m.UpdatedByAdmin)
                .Build();
            var parameters = new DbParameter[] { 
                    this._db.CreateParameter("PageIndex", pageIndex, DbType.Int32),
                    this._db.CreateParameter("PageSize", pageSize, DbType.Int32),
                    this._db.CreateParameter("TotalCounts", 0, DbType.Int32, ParameterDirection.Output),
                    this._db.CreateParameter("OrderClause", orderClause, DbType.String),
                    this._db.CreateParameter("WhereClause", whereClause, DbType.String)
                };
            var lst = this._db.Execute("SP_StaticContentGroup_GetList", map, parameters).ToList();
            total = (int)parameters[2].Value;

            return lst;
        }

        public StaticContentGroup GetDetail(int id, bool getInactive = true)
        {
            return StaticContentGroups.FirstOrDefault(a => a.Id == id && (a.IsActive == true || a.IsActive == !getInactive));
        }

        public void Save(StaticContentGroup StaticContentGroup, int actorId)
        {
            bool needToSave = false;
            if (StaticContentGroup.Id == -1)
            {
                StaticContentGroup.CreatedBy = actorId;
                StaticContentGroup.CreatedOn = DateTime.Now;
                context.StaticContentGroups.Add(StaticContentGroup);
                needToSave = true;
            }
            else
            {
                StaticContentGroup dbEntry = GetDetail(StaticContentGroup.Id);
                if (dbEntry != null)
                {
                    dbEntry.Name = StaticContentGroup.Name;
                    dbEntry.Description = StaticContentGroup.Description;
                    dbEntry.UpdatedOn = DateTime.Now;
                    dbEntry.UpdatedBy = actorId;
                    dbEntry.IsActive = StaticContentGroup.IsActive;
                    needToSave = true;
                }
            }
            if (needToSave)
            {
                context.SaveChanges();
            }
        }

        public int Delete(int[] ids)
        {
            bool needSaveChange = false;
            foreach (var id in ids)
            {
                StaticContentGroup dbEntry = GetDetail(id);
                if (dbEntry != null)
                {
                    context.StaticContentGroups.Remove(dbEntry);
                    needSaveChange = true;
                }
            }
            if (needSaveChange)
            {
                return context.SaveChanges();
            }
            else
            {
                return 0;
            }
        }

        public int UpdateActive(int[] arrId, bool isActive)
        {
            bool needToSave = false;
            foreach (var id in arrId)
            {
                StaticContentGroup dbEntry = GetDetail(id);
                if (dbEntry != null)
                {
                    dbEntry.IsActive = isActive;
                    needToSave = true;
                }
            }
            if (needToSave)
            {
                return context.SaveChanges();
            }
            else
            {
                return 0;
            }
        }

    }
}
